package great.project.jpa.signle.table;

import great.project.jpa.sign.table.SpringDataJpaSearchSingelTableDemo;
import great.project.jpa.sign.table.domain.entity.SysRole;
import great.project.jpa.sign.table.domain.respository.SysRoleRepository;
import org.hamcrest.MatcherAssert;
import org.hamcrest.collection.IsIn;
import org.hamcrest.core.Is;
import org.hamcrest.core.IsNull;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;

import javax.annotation.Resource;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * <div  style="border:2px solid none;margin:20px;padding:15px;font-size:24px;text-align:center;color:aqua;">
 * <p>
 *  内置的方法
 *
 *  </div>
 *
 * @author v_zhangshun01
 * @date 2021/8/20 11:12 上午
 */
@SpringBootTest(classes = SpringDataJpaSearchSingelTableDemo.class)
public class InnerMethod {

    @Resource
    private SysRoleRepository sysRoleRepository;

    /**
     * 查询全部
     */
    @Test
    void repositoryFindAll() {
        final List<SysRole> all = sysRoleRepository.findAll();
        MatcherAssert.assertThat(all.size(), Is.is(5));
    }

    @Test
    void repositoryFindAllWithIds() {
        final List<SysRole> allById =
                sysRoleRepository.findAllById(Arrays.asList("1169504891467464705", "1260924539346472962"));
        MatcherAssert.assertThat(
                allById.get(0).getId(),
                IsIn.in(Arrays.asList("1169504891467464705", "1260924539346472962"))
        );
        MatcherAssert.assertThat(
                allById.get(1).getId(),
                IsIn.in(Arrays.asList("1169504891467464705", "1260924539346472962"))
        );
    }

    @Test
    void repositoryFindById() {
        final Optional<SysRole> byId = sysRoleRepository.findById("1169504891467464705");
        MatcherAssert.assertThat(byId.isPresent(), Is.is(true));
    }

    @Test
    void repositoryFindExistsById() {
        final boolean b = sysRoleRepository.existsById("1169504891467464705");
        MatcherAssert.assertThat(b, Is.is(true));
    }

    @Test
    void repositoryFindAllSortById() {
        final List<SysRole> all = sysRoleRepository.findAll(Sort.by(Sort.Order.asc("id")));
        MatcherAssert.assertThat(all.get(0).getId(), Is.is("1169504891467464705"));
    }

    // ************************************************ 动态参数


    @Test
    void repositoryFindByUsernameIfNotNull() {
        sysRoleRepository.findAll((root, query, cb) -> {
            // 动态 sql
            final Predicate conjunction = cb.conjunction();
            //动态 SQL 表达式集合
            final List<Expression<Boolean>> expressions = conjunction.getExpressions();

            return conjunction;
        });
    }


    @Test
    void repositoryFindByRealNameIfNotNull() {
        sysRoleRepository.findAll((root, query, cb) -> {
            List<Predicate> list = new ArrayList<>();
            Predicate[] pre = new Predicate[list.size()];

            return query.where(list.toArray(pre)).getRestriction();
        });
    }


    @Test
    void findUser() {

        List<String> list = Arrays.asList("admin", null);

        list.forEach(this::doFindUser);


    }

    void doFindUser(String username){
        final Collection<SysRole> collection = sysRoleRepository.findAll((root, query, cb) -> {
            // 动态 sql
            final Predicate conjunction = cb.conjunction();
            //动态 SQL 表达式集合
            final List<Expression<Boolean>> expressions = conjunction.getExpressions();

            if (username != null) {
                expressions.add(cb.like(root.get("roleCode"), "%" + username + "%"));
            }

            return conjunction;
        });

        MatcherAssert.assertThat(collection, IsNull.notNullValue());
        System.out.println(collection);
    }

}
